local wm_daowu = fk.CreateSkill {
  name = "wm_daowu",
  tags = { Skill.Compulsory, },
}


local putEquipHandle = function(who, type, id, proposer, skiname)
  local room = who.room
  local eid = who:getEquipment(type)
  room:moveCards({
    from = who,
    ids = { id },
    toArea = Card.Processing,
    moveReason = fk.ReasonJustMove,
    proposer = who,
    skillName = skiname,
  })
  local move3 = {
    ids = { id },
    fromArea = Card.Processing,
    to = who,
    toArea = Card.PlayerEquip,
    moveReason = fk.ReasonJustMove,
    proposer = proposer or who,
    skillName = skiname,
  }

  if eid ~= nil then
    local move2 = {
      ids = { eid },
      from = who,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonJustMove,
    }
    room:moveCards(move2, move3)
  else
    room:moveCards(move3)
  end
end


wm_daowu:addEffect(fk.RoundStart, {
  priority = 10,
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wm_daowu.name) then return end
    local card = player:getEquipment(Card.SubtypeWeapon)
    return not card or Fk:getCardById(card).trueName ~= "blade"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local totalcards = Fk:getAllCardIds()
    for _, id in ipairs(totalcards) do
      local card = Fk:getCardById(id, true)
      if card.name == "blade" then
        local area = room:getCardArea(card.id)
        if area == Player.Hand or area == Player.Equip then
          room:obtainCard(player, id, true, fk.ReasonJustMove)
          putEquipHandle(player, Card.SubtypeWeapon, id, player, wm_daowu.name)
        end
        break
      end
    end
  end,
})

wm_daowu:addEffect(fk.CardEffectCancelledOut, {
  priority = 10,
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wm_daowu.name) then return end
    return data.from == player and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "daowu_extra-turn", 1)
  end,
})

wm_daowu:addEffect(fk.TargetSpecifying, {
  priority = 10,
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(wm_daowu.name) then return end
    return target == player and not player.dead and data.card.trueName == "slash" and
      player:getMark("daowu_extra-turn") > 0 and #data:getExtraTargets() > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = data:getExtraTargets()
    if #targets == 0 then return false end
    local tars = player:getMark("daowu_extra-turn")
    local tos = room:askToChoosePlayers(player,{
      min_num = 1,
      max_num = tars,
      targets = targets,
      prompt = "#wm_daowu-choose:::" .. data.card:toLogString() .. ":" .. tars,
      skill_name = wm_daowu.name,
      cancelable = true,
    })
    if #tos > 0 then
      for _, p in ipairs(tos) do
        data:addTarget(p)
      end
    end
  end,
})


return wm_daowu
